package com.vogue.supplymall.cross.domain.service.impl;

import com.vogue.ssm.database.dao.BaseDao;
import com.vogue.supplymall.common.domain.dto.admin.OrganManagementInfo;
import com.vogue.supplymall.common.domain.dto.admin.ShopSearchInfo;
import com.vogue.supplymall.cross.domain.dao.AdminManagementDao;
import com.vogue.supplymall.cross.domain.service.AdminManagementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Open shop service.
 *
 * @aouthor: zhangliang
 * @date: Create in 2018-05-15
 */
@Service("adminManagementService")
public class AdminManagementServiceImpl extends BaseServiceImpl<List, Long> implements AdminManagementService {
    /**
     * The constant log.
     */
    public static final Logger LOGGER_ADMINMANAGEMENTSERVICEIMPL = LoggerFactory.getLogger(AdminManagementServiceImpl.class);

    @Resource
    private AdminManagementDao adminManagementDao;

    /**
     * Instantiates a new Open shop service.
     *
     * @param baseDao the base dao
     */
    public AdminManagementServiceImpl(BaseDao<List, Long> baseDao) {
        super(baseDao);
    }

    @Override
    public Map<String, Object> findNotOpenShop(ShopSearchInfo shopSearchInfo) {
        LOGGER_ADMINMANAGEMENTSERVICEIMPL.info(" cross 查找未开通集团 ");
        if (shopSearchInfo.getPages() == 0) {
            return null;
        }
        shopSearchInfo.setStart((shopSearchInfo.getPages() - 1) * shopSearchInfo.getLength());
        // 开通店铺-查询集团申请开通的店铺
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> shopInfo;
        Long totalPages = adminManagementDao.findNotOpenShopCount(shopSearchInfo);
        data.put("totalPages", totalPages);
        if (totalPages > 0L) {
            shopInfo = adminManagementDao.findNotOpenShop(shopSearchInfo);
            for (Map<String, Object> objectMap : shopInfo) {
                String liu = objectMap.get("liu").toString();
                String[] liuArr = liu.split(",");
                for (String str : liuArr) {
                    String[] value = str.split(";");
                    Map<String, Object> licence = new HashMap<>();
                    if (value.length > 0) {
                        licence.put("licencetypeid", value[0]);
                        licence.put("id", value[1]);
                        licence.put("begindate", value[2]);
                        licence.put("enddate", value[3]);
                        licence.put("applystatue", value[4]);
                        licence.put("url", value[5]);
                        objectMap.put("licence" + value[0], licence);
                    }
                }
                objectMap.remove("liu");
                if (null == objectMap.get("checkdate")) {
                    objectMap.put("checkdate", "");
                }
            }
            data.put("lists", shopInfo);
        }
        return data;
    }

    @Override
    public List<Map<String, Object>> findOpenedShopWithOrgan(Map<String, Object> feature) {
        // 查看是否有还开通的店铺
        String sql = " select count(s.disabledflag) cnt "
                + " from match_organ mo left join shop s on(mo.id = s.organid) where mo.disabledflag = 0 and mo.syscodeid = #{syscodeid} ";
        return adminManagementDao.query0(sql, feature);
    }

    @Override
    public Map<String, Object> findOpenedOrgan(OrganManagementInfo info) {
        LOGGER_ADMINMANAGEMENTSERVICEIMPL.info(" cross 查找已开通集团 ");
        if (info.getPages() == 0) {
            return null;
        }
        info.setStart((info.getPages() - 1) * info.getLength());
        // 开通店铺-查询集团申请开通的店铺
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> organInfo;
        Long totalPages = adminManagementDao.findOpenedOrganCount(info);
        data.put("totalPages", totalPages);
        if (totalPages > 0L) {
            organInfo = adminManagementDao.findOpenedOrgan(info);
            for (Map<String, Object> objectMap : organInfo) {
                if (null == objectMap.get("opendate")) {
                    objectMap.put("opendate", "");
                }
            }
            data.put("lists", organInfo);
        }
        return data;
    }
}
